/*
 * This servlet controls the notes which a user makes during a lesson 
 */
package controllers.managecontrollers;

import classes.HibernateUtil;
import dao.LessonDao;
import dao.NoteDao;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import models.Lesson;
import models.Note;
import models.User;

/**
 *
 * @author Achie
 */
public class ManageNoteController extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //Get lesson object where written the note
        long lessonid = Long.parseLong(request.getParameter("lessonid"));
        HibernateUtil hu = new HibernateUtil();
        NoteDao nd = new NoteDao(hu.openSession());
        LessonDao ld = new LessonDao(hu.openSession());
        Lesson lesson = ld.get(lessonid); 

        //Get user object whom written the note
        HttpSession ses = request.getSession();
        User usr = (User) ses.getAttribute("user");

        //Get text inserted
        String text = request.getParameter("text");

        //-----------------------------------------------------
        //Check if note is an update/change
        //-----------------------------------------------------
        if (nd.findAllByUserAndLesson(usr.getMember_id(), lessonid).size() > 0) { //if the note from the user does exist
            //-----------------------------------------------------
            //Update note
            //-----------------------------------------------------
            Note note = nd.findAllByUserAndLesson(usr.getMember_id(), lessonid).get(0);
            note.setText(text);
            note.setDate();
            nd.update(note);
        } else {
            //-----------------------------------------------------
            //Save note
            //-----------------------------------------------------
            long id = System.nanoTime();
            Note note = new Note(id, text, lesson, usr); //create note object
            nd.save(note);
        }
        
        //REDIRECT--------------------------------------------------------------------------------
        response.sendRedirect("LectureController?id=" + lessonid);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
